import request from '@/router/axios';
import * as qs from 'qs'

const api = {
  worksheetInfo: '/api/Cgs/Worksheet/GetWorksheetInfo',
  addWorksheet: '/api/app/worksheet/info/save',
  addRow: '/api/app/worksheet/rows/save',
  getRow: '/api/app/worksheet/rows/get',
  deleteWorksheet: '/api/app/worksheet/info/delete',
  deleteRow: '/api/app/worksheet/rows/delete',
  updateWorksheetName: '/api/Cgs/Worksheet/UpdateName',
  updateWorksheet: '/api/app/worksheet/info/update',
  updateRow: '/api/app/worksheet/rows/update',
  getWorksheetDetail: '/api/app/worksheet/info/get',
  getFilterDataList: '/api/app/worksheet/rows/list',
  uploadExcel: '/api/app/worksheet/info/upload-excel',
  previewExcel: '/api/app/worksheet/info/preview-excel',
  importExcel: '/api/app/worksheet/info/import-excel',
  exportExcel: '/api/app/worksheet/info/export-excel',
  deleteRowBatch: '/api/app/worksheet/rows/batch-delete',
  updateRowBatch: '/api/app/worksheet/rows/batch-update',

  getWorksheetList: '/api/app/worksheet/info/listByAppId',

  // 级联选择器
  cascade: '/api/Cgs/Portal/Cascade',
  // secret
  listSecret: '/api/sdk/ListKey',
  editSecret: '/api/sdk/EditKey',
  addSecret: '/api/sdk/AddKey',
  deleteSecret: '/api/sdk/RemoveKey',

  getApiData: '/api/worksheet/getApiData',

  // 新建表单from明道
  getMdApp: '/api/Cgs/md/GetApp',
  getMdWorksheetInfo: '/api/Cgs/md/GetWorksheetInfo',
  saveMdStruct: '/api/Cgs/md/SaveMDStruct',

  //视图
  addView: '/api/app/worksheet/view/save',
  deleteView: '/api/app/worksheet/view/delete',
  updateView: '/api/app/worksheet/view/update',
  getViewDate: '/api/app/worksheet/view/get',

  //公开
  worksheetPublicDetail: '/api/app/worksheet/public/get',
  updateWorksheetPublic: '/api/app/worksheet/public/update',
  savePublicRow: '/api/app/worksheet/rows/savePublic',
  addWorksheetPublic: '/api/app/worksheet/public/ext/addUrl',
  deleteWorksheetPublic: '/api/app/worksheet/public/ext/deleteUrl',

  //分享表单二维码
  worksheetewm: '/api/generateQRCode',
}

export function worksheetewm(data) {
  return new Promise((resolve, reject) => {
    request({
      url: api.worksheetewm,
      method: 'post',
      data: data,
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
    }).then((result) => {
      if (result?.data.success) {
        resolve(result?.data.data)
      } else {
        resolve(false)
      }
    }).catch((err) => {
      reject(err)
    });
  })
}

export function getWorksheetList(parameter) {
  return request({
    url: api.getWorksheetList,
    method: 'get',
    params: parameter
  })
}

export function getCascadeData(parameter) {
  return request({
    url: api.cascade,
    method: 'get',
    params: parameter
  })
}

export function getAppCascadeData() {
  return new Promise((resolve, reject) => {
    request({
      url: api.cascade + `?visualType=App&getAllWorksheet=true`,
      method: 'get'
    }).then((result) => {
      if (result?.data.code === 200) {
        resolve(result.data.data)
      } else {
        reject(new Error("未请求到表单信息"))
      }
    }).catch((err) => {
      reject(err)
    });
  })
}

export function getWorksheetPublicDetail({worksheetId, shareId}) {
  let url = api.worksheetPublicDetail + `?worksheetId=${worksheetId}`
  if(!worksheetId){
    url = api.worksheetPublicDetail + `?shareId=${shareId}`
  }
  return new Promise((resolve, reject) => {
    request({
      url,
      method: 'get'
    }).then((result) => {
      if (result?.data.success) {
        resolve(result.data.data)
      } else {
        reject(new Error("未请求到表单公布详情"))
      }
    }).catch((err) => {
      reject(err)
    });
  })
}

export function updateWorksheetPublic(data) {
  return new Promise((resolve, reject) => {
    request({
      url: api.updateWorksheetPublic,
      method: 'post',
      data: data,
      headers: {
        'Content-Type': 'application/json'
      },
    }).then((result) => {
      if (result?.data.success) {
        resolve(true)
      } else {
        resolve(false)
      }
    }).catch((err) => {
      reject(err)
    });
  })
}

export function savePublicRow(data) {
  return new Promise((resolve, reject) => {
    request({
      url: api.savePublicRow,
      method: 'post',
      data: data,
      headers: {
        'Content-Type': 'application/json'
      },
    }).then((result) => {
      if (result?.data.success) {
        resolve(true)
      } else {
        resolve(false)
      }
    }).catch((err) => {
      reject(err)
    });
  })
}

// 添加公共表单
export function addWorksheetPublic(data) {
  return new Promise((resolve, reject) => {
    request({
      url: api.addWorksheetPublic,
      method: 'post',
      data: data,
      headers: {
        'Content-Type': 'application/json'
      },
    }).then((result) => {
      if (result?.data.success) {
        resolve(result?.data.data)
      } else {
        resolve(false)
      }
    }).catch((err) => {
      reject(err)
    });
  })
}

// 删除公共表单
export function deleteWorksheetPublic({id}) {
  let url = api.deleteWorksheetPublic + `?id=${id}`
  return new Promise((resolve, reject) => {
    request({
      url,
      method: 'post'
    }).then((result) => {
      if (result?.data.success) {
        resolve(result.data.data)
      } else {
        reject(new Error("未请求到表单公布详情"))
      }
    }).catch((err) => {
      reject(err)
    });
  })
}

export function getRow(parameter) {
  return new Promise((resolve, reject) => {
    request({
      url: api.getRow,
      method: 'get',
      params: parameter
    }).then((result) => {
      if (result?.data.success) {
        resolve(result.data.data)
      } else {
        reject(new Error("未请求到该行数据"))
      }
    }).catch((err) => {
      reject(err)
    });
  })
}

export function getShareRow(parameter) {
  return new Promise((resolve, reject) => {
    request({
      url: api.getRow,
      method: 'get',
      params: parameter,
      headers: {
        'X-Mode': 'share'
      }
    }).then((result) => {
      if (result?.data.success) {
        resolve(result.data.data)
      } else {
        reject(new Error("未请求到该行数据"))
      }
    }).catch((err) => {
      reject(err)
    });
  })
}

export function getWorksheetInfo(parameter) {
  return new Promise((resolve, reject) => {
    request({
      url: api.worksheetInfo,
      method: 'get',
      params: parameter
    }).then((result) => {
      if (result?.data.success) {
        resolve(result.data.data)
      } else {
        reject(new Error("未请求到工作表详情"))
      }
    }).catch((err) => {
      reject(err)
    });
  })
}

export function getWorksheetDetail(id) {
  return new Promise((resolve, reject) => {
    request({
      url: api.getWorksheetDetail,
      method: 'get',
      params: { id: id }
    }).then((result) => {
      if (result?.data.success) {
        resolve(result.data.data)
      } else {
        reject(new Error("未请求到工作表详情"))
      }
    }).catch((err) => {
      reject(err)
    });
  })
}

export function getShareWorksheetDetail(id) {
  return new Promise((resolve, reject) => {
    request({
      url: api.getWorksheetDetail,
      method: 'get',
      params: { id: id },
      headers: {
        'X-Mode': 'share'
      }
    }).then((result) => {
      if (result?.data.success) {
        resolve(result.data.data)
      } else {
        reject(new Error("未请求到工作表详情"))
      }
    }).catch((err) => {
      reject(err)
    });
  })
}

export function getFilterDataList(data) {
  if(!data.sortField){
    data.sortField = 'updateTime'
  }
  return new Promise((resolve, reject) => {
    request({
      url: api.getFilterDataList,
      method: 'post',
      data: data,
      headers: {
        'Content-Type': 'application/json'
      },
      timeout: 1000 * 60
    }).then((result) => {
      if (result?.data.success) {
        resolve(result.data)
      } else {
        resolve([])
      }
    }).catch((err) => {
      reject(err)
    });
  })
}

export function getShareFilterDataList(data) {
  if(!data.sortField){
    data.sortField = 'updateTime'
  }
  return new Promise((resolve, reject) => {
    request({
      url: api.getFilterDataList,
      method: 'post',
      data: data,
      headers: {
        'Content-Type': 'application/json',
        'X-Mode': 'share'
      },
      timeout: 1000 * 60
    }).then((result) => {
      if (result?.data.success) {
        resolve(result.data)
      } else {
        reject(new Error("未请求到工作表数据"))
      }
    }).catch((err) => {
      reject(err)
    });
  })
}

export function getModelList(data) {
  return new Promise((resolve, reject) => {
    request({
      url: '/api/v2/worksheet/getFilterRows',
      method: 'post',
      data: data,
      headers: {
        'Content-Type': 'application/json'
      }
    }).then((result) => {
      if (result?.data.success) {
        resolve(result.data.data)
      } else {
        reject(new Error("未请求到工作表数据"))
      }
    }).catch((err) => {
      reject(err)
    });
  })
}

export function addWorksheet(data) {
  return new Promise((resolve, reject) => {
    request({
      url: api.addWorksheet,
      method: 'post',
      data: data,
      headers: {
        'Content-Type': 'application/json'
      }
    }).then((result) => {
      if (result?.data.success) {
        resolve(result.data.data)
      } else {
        reject(new Error("工作表添加失败"))
      }
    }).catch((err) => {
      reject(err)
    });
  })
}

export function addRow(data) {
  return new Promise((resolve, reject) => {
    request({
      url: api.addRow,
      method: 'post',
      data: data,
      headers: {
        'Content-Type': 'application/json'
      }
    }).then((result) => {
      if (result?.data.success) {
        resolve(result.data.data)
      } else {
        reject(new Error("添加记录失败"))
      }
    }).catch((err) => {
      reject(err)
    });
  })
}

export function deleteWorksheet(id) {
  return new Promise((resolve, reject) => {
    request({
      url: api.deleteWorksheet,
      method: 'post',
      params: { id: id }
    }).then((result) => {
      if (result?.data.success) {
        resolve(result.data.data)
      } else {
        reject(new Error("工作表删除失败"))
      }
    }).catch((err) => {
      reject(err)
    });
  })
}

export function deleteRow(params) {
  return new Promise((resolve, reject) => {
    request({
      url: api.deleteRow,
      method: 'post',
      params: params,
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      }
    }).then((result) => {
      if (result?.data.success) {
        resolve(result.data.data)
      } else {
        reject(new Error("工作表删除失败"))
      }
    }).catch((err) => {
      reject(err)
    });
  })
}

export function deleteRowBatch(data) {
  return new Promise((resolve, reject) => {
    request({
      url: api.deleteRowBatch,
      method: 'post',
      data: data,
      headers: {
        'Content-Type': 'application/json'
      }
    }).then((result) => {
      if (result?.data.success) {
        resolve()
      } else {
        reject(new Error("批量删除失败"))
      }
    }).catch((err) => {
      reject(err)
    });
  })
}

export function updateRowBatch(data) {
  return new Promise((resolve, reject) => {
    request({
      url: api.updateRowBatch,
      method: 'post',
      data: data,
      headers: {
        'Content-Type': 'application/json'
      }
    }).then((result) => {
      if (result?.data.success) {
        resolve()
      } else {
        reject(new Error("批量编辑失败"))
      }
    }).catch((err) => {
      reject(err)
    });
  })
}

export function updateWorksheetName(params) {
  return request({
    url: api.updateWorksheetName,
    method: 'post',
    params: params,
    paramsSerializer: params => {
      return qs.stringify(params, { indices: false })
    },
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  })
}

export function updateWorksheet(data) {
  return new Promise((resolve, reject) => {
    request({
      url: api.updateWorksheet,
      method: 'post',
      data: data,
      headers: {
        'Content-Type': 'application/json'
      }
    }).then((result) => {
      if (result?.data.success) {
        resolve(result.data)
      } else {
        reject(new Error("工作表修改失败"))
      }
    }).catch((err) => {
      reject(err)
    });
  })
}

export function updateRow(data) {
  return new Promise((resolve, reject) => {
    request({
      url: api.updateRow,
      method: 'post',
      data: data,
      headers: {
        'Content-Type': 'application/json'
      }
    }).then((result) => {
      if (result?.data.success) {
        resolve(result.data.success)
      } else {
        reject(new Error("修改行记录失败"))
      }
    }).catch((err) => {
      reject(err)
    });
  })
}

export function uploadExcel(data) {
  return new Promise((resolve, reject) => {
    request({
      url: api.uploadExcel,
      method: 'post',
      data: data,
      headers: {
        'Content-Type': 'multipart/form-data'
      },
      timeout: 1000 * 60 * 5
    }).then((result) => {
      if (result?.data.success) {
        resolve(result.data.data)
      } else {
        reject(new Error("excel上传失败"))
      }
    }).catch((err) => {
      reject(err)
    });
  })
}

export function previewExcel(params) {
  return new Promise((resolve, reject) => {
    request({
      url: api.previewExcel,
      method: 'get',
      params: params,
      timeout: 1000 * 60 * 5
    }).then((result) => {
      if (result?.data.success) {
        resolve(result.data.data)
      } else {
        reject(new Error("excel信息获取失败"))
      }
    }).catch((err) => {
      reject(err)
    });
  })
}

export function importExcel(data) {
  return new Promise((resolve, reject) => {
    request({
      url: api.importExcel,
      method: 'post',
      data: data,
      headers: {
        'Content-Type': 'application/json'
      },
      timeout: 1000 * 60 * 5
    }).then((result) => {
      if (result?.data.success) {
        resolve(result.data.data)
      } else {
        reject(new Error("excel导入失败"))
      }
    }).catch((err) => {
      reject(err)
    });
  })
}

export function exportExcel(data) {
  return new Promise((resolve, reject) => {
    request({
      url: api.exportExcel,
      method: 'post',
      data: data,
      headers: {
        'Content-Type': 'application/json;charset=utf-8'
      },
      responseType: 'blob',
      timeout: 1000 * 60 * 5
    }).then((result) => {
      if (result?.data) {
        resolve(result)
      } else {
        reject(new Error("excel导出失败"))
      }
    }).catch((err) => {
      reject(err)
    });
  })
}

export function getSecret(parameter = {}) {
  return request({
    url: api.listSecret,
    method: 'get',
    params: parameter
  })
}

export function editSecret(params = {}) {
  return request({
    url: api.editSecret,
    method: 'post',
    params: params,
    paramsSerializer: params => {
      return qs.stringify(params, { indices: false })
    },
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  })
}

export function addSecret() {
  return request({
    url: api.addSecret,
    method: 'post',
  })
}

export function deleteSecret(id) {
  return request({
    url: api.deleteSecret,
    method: 'post',
    params: { ID: id }
  })
}

export function getApiData() {
  return request({
    url: api.getApiData,
    method: 'get',
  })
}

export function getApp(params = {}) {
  return request({
    url: api.getMdApp,
    method: 'get',
    params: params,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  })
}

export function getMdWorksheet(params) {
  return request({
    url: api.getMdWorksheetInfo,
    method: 'post',
    params: params,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  })
}

export function saveMdStruct(data) {
  return request({
    url: api.saveMdStruct,
    method: 'post',
    data: data,
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

export function addView(data) {
  return new Promise((resolve, reject) => {
    request({
      url: api.addView,
      method: 'post',
      data: data,
      headers: {
        'Content-Type': 'application/json'
      }
    }).then((result) => {
      if (result?.data) {
        resolve(result.data.data)
      } else {
        reject(new Error("视图添加失败"))
      }
    }).catch((err) => {
      reject(err)
    });
  })
}

export function deleteView(id) {
  return new Promise((resolve, reject) => {
    request({
      url: api.deleteView + `?id=${id}`,
      method: 'post',
      headers: {
        'Content-Type': 'application/json'
      }
    }).then((result) => {
      if (result?.data) {
        resolve(result)
      } else {
        reject(new Error("视图删除失败"))
      }
    }).catch((err) => {
      reject(err)
    });
    
  })
}
export function updateView(data){
  return new Promise((resolve, reject) => {
    request({
      url: api.updateView,
      method: 'post',
      data: data,
      headers: {
        'Content-Type': 'application/json'
      }
    }).then((result) => {
      if (result?.data) {
        resolve(result)
      } else {
        reject(new Error("修改视图失败"))
      }
    }).catch((err) => {
      reject(err)
    });
  })
}
export function getViewDate(id){
  return new Promise((resolve, reject) => {
    request({
      url: api.getViewDate + `?id=${id}`,
      method: 'get',
    }).then((result) => {
      if (result) {
        resolve(result)
      } else {
        reject(new Error("获取视图数据失败"))
      }
    }).catch((err) => {
      reject(err)
    });
  })
}
